import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;
import LGXCOM from '../common/common.js'

import {LGXEnums}  from '../common/sysenum.js';



var AbsorbSizeSvc = {}



AbsorbSizeSvc.absorbToSetSameSize = function (pDataMng, mousePoint, originOffsetPt) {
    if (pDataMng.getMapWorkMode() != LGXEnums.LGXWorkMode.ModifyMode) {
        return null;
    }
    var pCurentGraph = pDataMng.getCurentGraph();
    if (!pCurentGraph || (pCurentGraph && pCurentGraph.getModifing() != 1)) {
        return null;
    }
    if (funcs.isDimGraph(pCurentGraph.getType())) {
        return null;
    }

    let auxDataMng = pDataMng.getAuxDataManager();
    auxDataMng.setSameSizeGroup(null,null,0);
    auxDataMng.setSameSizeGroup(null,null,1);
    let dragKind = pCurentGraph.getSelKind();
    switch (dragKind) {
        case LGXEnums.LGXModGraphKind.Drag_leftTop:
        case LGXEnums.LGXModGraphKind.Drag_rightTop:
        case LGXEnums.LGXModGraphKind.Drag_RightBottom:
        case LGXEnums.LGXModGraphKind.Drag_leftBottom:
            this.chkAbsorbOtherGraphSize(pDataMng, pCurentGraph,0);
            this.chkAbsorbOtherGraphSize(pDataMng, pCurentGraph,1);
            break;

        case LGXEnums.LGXModGraphKind.Drag_LeftMid:
        case LGXEnums.LGXModGraphKind.Drag_RightMid:
            this.chkAbsorbOtherGraphSize(pDataMng, pCurentGraph,0);
            break;

        case LGXEnums.LGXModGraphKind.Drag_TopMid:
        case LGXEnums.LGXModGraphKind.Drag_BottomMid:
            this.chkAbsorbOtherGraphSize(pDataMng, pCurentGraph,1);
            break;

        default:
            break;
    }
    return null;
}


AbsorbSizeSvc.chkAbsorbOtherGraphSize = function (pDataMng, pCurentGraph,kind) {
    let curentSize = 0;
    if (kind == 0){
        curentSize = pCurentGraph.getZone().width();
    }
    else if (kind == 1){
        curentSize = pCurentGraph.getZone().height();
    }
    else{
        return ;
    }
    var minDeta = -1;
    let sameSizeDstGraph = null;
    var graphLst = pDataMng.getAllGraphs();
    var count = graphLst.length;
    for (let i = 0; i < count; i++) {
        let pGraph = graphLst[i];
        if (!this.chkCanAbsorb(pCurentGraph, pGraph)) {
            continue;
        }
        let dstSize = pGraph.getZone().width();
        if (kind == 1){
            dstSize = pGraph.getZone().height();
        }
        let deta = Math.abs(curentSize - dstSize);
        if (deta < global.SelectGap / 4) {
            if (minDeta < 0) {
                minDeta = deta;
                sameSizeDstGraph = pGraph;
            }
            else if (minDeta > deta) {
                minDeta = deta;
                sameSizeDstGraph = pGraph;
            }
        }
    }
    let auxDataMng = pDataMng.getAuxDataManager();
    if (sameSizeDstGraph){
        auxDataMng.setSameSizeGroup(pCurentGraph,sameSizeDstGraph,kind);
    }
    else{
        auxDataMng.setSameSizeGroup(null,null,kind);
    }
    return ;
}

AbsorbSizeSvc.chkCanAbsorb = function (pCurentGraph, pGraph) {
    let flag = true;
    let group = funcs.group(pGraph.getType());
    let childList = pCurentGraph.getAllChild();
    if (pCurentGraph == pGraph
        || pGraph.getFinish() == 0
        || pGraph.getUseful() != 1
        || pGraph.getModifing() == 1
        || pGraph == pCurentGraph.getLabel()
        || group == global.dimGroup
        || childList.indexOf(pGraph) >= 0
    ) {
        return false;
    }

    if (pGraph.m_graphType == LGXEnums.LGXGraphType.LineGraph_type) {
        return false;
    }

    if (pGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
        return false;
    }

    if (pGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
        && pCurentGraph == pGraph.getParentGraph()) {
        return false;
    }

    if ((pGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
        && pCurentGraph.m_graphType != LGXEnums.LGXGraphType.TextGraph_type)
        || (pGraph.m_graphType != LGXEnums.LGXGraphType.TextGraph_type
            && pCurentGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type)) {
        return false;
    }

    return flag;
}



export default { AbsorbSizeSvc }


